using RockHopper.Common.Commanding;
using RockHopper.TrainingCalculator.WinRT.Utils;
using Windows.ApplicationModel;
using Windows.Foundation;
using Windows.Graphics.Display;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;

namespace RockHopper.TrainingCalculator.WinRT.Controls
{
    public class PopupControl : ContentControl
    {
        private Popup popup;
        private Button closeButton;
        private TextBlock title;

        public PopupControl()
        {
            DefaultStyleKey = typeof(PopupControl);
        }

        public static readonly DependencyProperty TitleProperty =
            DependencyPropertyDescriptor.CreateProperty<PopupControl, string>("Title");

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        public static readonly DependencyProperty IsOpenProperty =
            DependencyPropertyDescriptor.CreateProperty<PopupControl, bool>("IsOpen", false, OnIsOpenChangedCallback);

        public bool IsOpen
        {
            get { return (bool)GetValue(IsOpenProperty); }
            set { SetValue(IsOpenProperty, value); }
        }

        public static readonly DependencyProperty CloseCommandProperty =
            DependencyPropertyDescriptor.CreateProperty<PopupControl, DelegateCommand>("CloseCommand");

        private Rect bounds;

        public DelegateCommand CloseCommand
        {
            get { return (DelegateCommand)GetValue(CloseCommandProperty); }
            set { SetValue(CloseCommandProperty, value); }
        }

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            title = (TextBlock) GetTemplateChild("TitleTextPart");
            title.Text = Title ?? "";
            popup = (Popup) GetTemplateChild("PopupPart");

            closeButton = (Button)GetTemplateChild("CloseButtonPart");
            closeButton.Click += OnCloseClick;
            
            if (!DesignMode.DesignModeEnabled)
            {
                bounds = CoreWindow.GetForCurrentThread().Bounds;

                if (DisplayPropertiesHelper.IsPortrait)
                    bounds = new Rect(0, 0, bounds.Height, bounds.Width);

                UpdateForOrientation();
                DisplayProperties.OrientationChanged += s => UpdateForOrientation();
            }
        }

        private void OnCloseClick(object sender, RoutedEventArgs args)
        {
            if (CloseCommand != null && CloseCommand.CanExecute(this))
                CloseCommand.Execute(this);
        }

        private void UpdateForOrientation()
        {
            if (!DesignMode.DesignModeEnabled)
            {
                if (DisplayPropertiesHelper.IsPortrait)
                {
                    Width = bounds.Height;
                    Height = bounds.Width;
                }
                else
                {
                    Width = bounds.Width;
                    Height = bounds.Height;
                }
            }
        }

        private static void OnIsOpenChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
        {
            var control = (PopupControl)dependencyObject;
            control.popup.IsOpen = (bool)args.NewValue;
        }
    }
}